trigger AccountOORSecondaryOwnerUpdateTrigger on Account (before update) {
    
    List<Account> AccList = new List<Account>();
    List<AccountTeamMember> AccountTeamMemberList = new List<AccountTeamMember>();
    List<AccountTeamMember> AccountTeamMemberDelete = new List<AccountTeamMember>();
    Set<Id> AccountIds = new Set<Id>();
    List<Account> AccountIdlist = new List<Account>();
    List<Opportunity> OpportunityList= new List<Opportunity>();
    ID SFAOORRenewalecordTypeID = RecordTypeHelper.GetRecordTypeID('Opportunity', 'SFA-OOR Renewals');
    Map<Id,User> UserMap = new Map<Id,User>();
    for(User singleuser:[Select id,FirstName,LastName,SFA_Office__c,Manager.FirstName,Manager.LastName,ManagerId,UserRole.Name from User where SFA_Office__c != NULL AND ISACTIVE = TRUE]){ 
        UserMap.put(singleuser.Id,singleuser);    
     } 
     for(Account acc:Trigger.new){
        if(acc.Secondary_Owner__c!=null && (acc.Secondary_Owner__c!=Trigger.oldMap.get(acc.id).Secondary_Owner__c)){
            
            AccList.add(acc);
            AccountIds.add(acc.id);
            
           }
        }
         for(integer i=0;i<AccList.size();i++){
             AccountTeamMember AccMemb = new AccountTeamMember();
             AccMemb.AccountId = AccList[i].id;
             AccMemb.UserId = AccList[i].Secondary_Owner__c;
             AccountTeamMemberList.add(AccMemb);
                  
    }
    if(AccountTeamMemberList.size()>0){
       try{
        AccountTeamMemberDelete = [select id from AccountTeamMember where AccountId IN: AccountIds];
       delete AccountTeamMemberDelete;
       Insert AccountTeamMemberList;
       
       }
        
    catch (System.DmlException e) {
        for (Integer i = 0; i < e.getNumDml(); i++) {
              System.debug(e.getDmlMessage(i));
    }   
 }
  }
  for(Account accs:Trigger.new){
      if(accs.Recurring_Charge__c != Trigger.OldMap.get(accs.Id).Recurring_Charge__c){
          accs.Previous_AF_Assigned__c = Trigger.OldMap.get(accs.Id).Recurring_Charge__c;
      }
      if(accs.Last_Billed_Date__c != Trigger.OldMap.get(accs.Id).Last_Billed_Date__c && accs.Type__c == 'Evergreen'){
         AccountIdlist.add(accs);
      }
  }
  for(Account acc: AccountIdlist){
      
      Opportunity Opp = New Opportunity();
      Opp.RecordTypeId = SFAOORRenewalecordTypeID;
      Opp.Name = acc.Name;
      Opp.AccountId = acc.Id;
      //Commented Below Code to fix Active Inacitve User issue [SZ]
      ListViewIds__c adminId = ListViewIds__c.getInstance();   
      Opp.ownerId=adminId.SalesforceAdmin__c; 
      //Opp.OwnerId = acc.OwnerId;
      //Start : - Added  for resolve Active inactive Owner Issue[SZ]
      // Opp.OwnerId = Uncomment this line .Please assign dummy salesforce admin ID. Need to store this ID in Custom setting.
       Opp.Ext_Primary_Owner__c=acc.OwnerId;
      //End : - Added  for resolve Active inactive Owner Issue[SZ]
      
      Opp.Opportunity_Type__c = 'Bill Cycle Evergreen';
      Opp.StageName = 'Completed';
      Opp.Probability = 1;
      Opp.Revenue_mo__c = acc.Last_Evergreen_Billed__c;
      Opp.CloseDate = acc.Last_Billed_Date__c;
      Opp.SOLO_Order_State__c = 'C';
      Opp.Order_Date_SOLO__c = acc.Last_Billed_Date__c;
      Opp.SOLO_Contract_Date__c = acc.Last_Billed_Date__c;
      Opp.Order_Completion_Date__c = acc.Last_Billed_Date__c;
       //Commented Below Code to fix Active Inacitve User issue [SZ]
      //opp.Secondary_Owner__c=acc.OOR_Secondary_Account_Owner__c;
      //Start : - Added  for resolve Active inactive Owner Issue[SZ]
      Opp.Ext_Secondary_Owner__c=acc.OOR_Secondary_Account_Owner__c;
      //End : - Added  for resolve Active inactive Owner Issue[SZ]
      Opp.SOLO_Submitted_Date__c = acc.Last_Billed_Date__c;
      Opp.Monthly_Amount__c = acc.Last_Evergreen_Billed__c;
      Opp.SOLO_Contract_Revenue_mo__c = acc.Last_Evergreen_Billed__c;
      Opp.SOLO_Prev_Monthly_Amt__c = 0;
      opp.SOLO_Manager_P_Owner__c = UserMap.get(acc.OwnerId).Manager.FirstName+' '+UserMap.get(acc.OwnerId).Manager.LastName;  
      opp.SOLO_Office_P_Owner_Code__c = UserMap.get(acc.OwnerId).SFA_Office__c;     
      if(acc.OOR_Secondary_Account_Owner__c!=NULL){   
        if(UserMap.containsKey(acc.OOR_Secondary_Account_Owner__c)){   
         opp.SOLO_Office_S_Owner_Code__c=UserMap.get(acc.OOR_Secondary_Account_Owner__c).SFA_Office__c;   
              if(UserMap.get(acc.OOR_Secondary_Account_Owner__c).ManagerId!=null){  
              opp.SOLO_Manager_S_Owner__c=UserMap.get(acc.OOR_Secondary_Account_Owner__c).Manager.FirstName+' '+UserMap.get(acc.OOR_Secondary_Account_Owner__c).Manager.LastName;
            }
       }
    }
        
      OpportunityList.add(Opp);
            
     }
      if(OpportunityList.size()>0){
          insert OpportunityList;
      }
          
    }